package com.tenton.servlet;

import com.tenton.pojo.User;
import com.tenton.service.Impl.UserServiceImpl;
import com.tenton.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Date: 2021/1/18
 * @Author: Tenton
 * @Description: 修改用户信息
 */
@WebServlet(name = "UpdateUserServlet",urlPatterns = "/updateUserServlet")
public class UpdateUserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码格式，防止乱码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        UserService userService = new UserServiceImpl();
        //获取前端注册页面表单数据
        //用户Id
        int id = Integer.parseInt(request.getParameter("id"));
        //用户名
        String userName = request.getParameter("userName");
        //密码
        String password = request.getParameter("password");
        //姓名
        String fullName = request.getParameter("fullName");
        //电话
        String tel = request.getParameter("tel");
        //邮箱
        String email = request.getParameter("email");
        //兴趣爱好
        String hobby = request.getParameter("hobby");
        //密保问题
        String problem = request.getParameter("problem");
        //密保答案
        String answer = request.getParameter("answers");
        //地址
        String address = request.getParameter("address");
        //所在国家
        String country = request.getParameter("country");
        //所在城市
        String city = request.getParameter("city");
        User user = userService.getUserById(id);
        user = new User(id,userName, password, fullName, tel, address, country, city, email,hobby,problem,answer);
        if (id != 1) {
            userService.updateUser(user);
        }else {
            response.getWriter().write("此为管理员账户，不能修改，2秒后自动跳转到主界面");
            response.setHeader("Refresh","3;URL=/listUserServlet");
            return;
        }
        response.sendRedirect(request.getContextPath() + "/listUserServlet");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
